﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ESOL.Business.Mst;
using ESOL.Business.Inv;
using ESOL.Business.Fin;
using ESOL.Common;
using ESOL.Common.Entity;
using ESOL.WinUI.MasterData;
using ESOL.WinUI.UICommon;
using ESOL.WinUI.Sales;
using ESOL.WinUI.Pur;
using ESOL.Controls;
using ESOL.WinUI.Hr;
using ESOL.WinUI.Inv;

namespace ESOL.WinUI.Finance
{
    public partial class frmMaintainAPRequest : BaseForm.frmBaseDialogyBusinessForm
    {
        #region Variable
        public FinBillingPaidApplicationEntity SelectedBillingApplication { get; set; }
        private List<InvImportItemEntity> mListImportItem;
        private bool mIsPaid = false;
        private FinPaidDetailEntity mPaidDetail;
        private FinPaidDetailEntity PaidDetail
        {
            get
            {
                if (mPaidDetail == null)
                {
                    if (SelectedBillingApplication == null || string.IsNullOrEmpty(SelectedBillingApplication.ApplicationId))
                    {
                        mPaidDetail = null;
                    }
                    else
                    {
                        mPaidDetail = FinPaidDetailBs.GetByApplicationId(SelectedBillingApplication.ApplicationId);
                    }
                }
                return mPaidDetail;
            }
            set
            {
                mPaidDetail = value;
            }
        }
        #endregion

        #region Constructor
        public frmMaintainAPRequest()
        {
            InitializeComponent();
            if (SelectedBillingApplication == null)
            {
                SelectedBillingApplication = new FinBillingPaidApplicationEntity();
                SelectedBillingApplication.ApplicationId = Guid.NewGuid().ToString();
            }
        }
        public frmMaintainAPRequest(bool isPaid)
        {
            InitializeComponent();
            mIsPaid = isPaid;
            if (SelectedBillingApplication == null)
            {
                SelectedBillingApplication = new FinBillingPaidApplicationEntity();
                SelectedBillingApplication.ApplicationId = Guid.NewGuid().ToString();
            }
        }
        public frmMaintainAPRequest(List<InvImportItemEntity> list, MstSupplierEntity supplier)
        {
            InitializeComponent();
            if (SelectedBillingApplication == null)
            {
                SelectedBillingApplication = new FinBillingPaidApplicationEntity();
                SelectedBillingApplication.ApplicationId = Guid.NewGuid().ToString();
            }

            mListImportItem = list;
            txtSupplier.Tag = supplier;
        }
        #endregion

        #region Functions
        private void SetFieldsBySupplier(MstSupplierEntity supplier)
        {
            if (supplier != null)
            {
                txtSupplier.Tag = supplier;
                txtSupplier.Text = supplier.SupplierName;
                UICommonFunctions.BindContactCombobox(cmbSupplierContact, supplier.SupplierId, false);
                if (cmbSupplierContact.Items.Count > 0)
                {
                    cmbSupplierContact.SelectedIndex = 0;
                    MstSupplierContactEntity entity = (MstSupplierContactEntity)cmbSupplierContact.SelectedItem;
                    if (entity != null)
                    {
                        txtSupplierContactPhone.Text = entity.ContactMobilephone;
                    }
                }
            }
            else
            {
                txtSupplier.Tag = null;
                txtSupplier.Text = "";
                UICommonFunctions.BindContactCombobox(cmbSupplierContact, "", false);
                txtSupplierContactPhone.Text = "";
            }
        }
        private void BindComboBox()
        {
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbCurrencyApp, "CS", false);
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbPayTypeApp, "PAT", false);
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbStatusApp, "PAYING", false);
            UICommon.UICommonFunctions.BindComboboxByCommonItemWithCode(cmbOrderTypeApp, "CFT", false);

            //Paid information
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbCurrency, "CS", false);
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbPayType, "PAT", false);
            UICommon.UICommonFunctions.BindComboboxByCommonItemWithCode(cmbOrderType, "CFT", false);
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbPaidType, "PAYTYPE", false);

            DataTable dt = FinPaidDetailBs.GetBankAccount();
            if (dt != null)
            {
                cmbBankAccount.DataSource = dt;
                cmbBankAccount.DisplayMember = "bank_account";
                cmbBankAccount.ValueMember = "bank_account";
            }
        }
        private void SetFields()
        {
            if (SelectedBillingApplication == null)
            {
                return;
            }
            txtApplicationCode.Text = SelectedBillingApplication.ApplicationCode;
            if (string.IsNullOrEmpty(txtApplicationCode.Text))
            {
                txtApplicationCode.Text = "自动生成";
            }
            if (string.IsNullOrEmpty(SelectedBillingApplication.SupplierId) == false)
            {
                MstSupplierEntity supplier = MstSupplierBs.GetMstSupplierById(SelectedBillingApplication.SupplierId);
                SetFieldsBySupplier(supplier);
            }
            if (SelectedBillingApplication.ApplicationDate.ToString("yyyy-MM-dd") == "1900-01-01" ||
                SelectedBillingApplication.ApplicationDate.ToString("yyyy-MM-dd") == "0001-01-01")
            {
                dtpApplicationDate.EditValue = null;
            }
            else
            {
                dtpApplicationDate.EditValue = SelectedBillingApplication.ApplicationDate;
            }
            if (ThisActionType == ActionType.New)
            {
                if (CommonParameter.CurrentUser.PersonEntity != null)
                {
                    txtApplicationPerson.Tag = CommonParameter.CurrentUser.PersonEntity;
                    txtApplicationPerson.Text = CommonParameter.CurrentUser.PersonEntity.NameCn + " (" + CommonParameter.CurrentUser.PersonEntity.PersonCode + ")";
                }
            }
            else
            {
                if (string.IsNullOrEmpty(SelectedBillingApplication.ApplicationPerson) == false)
                {
                    MstPersonEntity person = MstPersonBs.GetMstPersonByID(SelectedBillingApplication.ApplicationPerson);
                    if (person != null)
                    {
                        txtApplicationPerson.Tag = person;
                        txtApplicationPerson.Text = person.NameCn + " (" + person.PersonCode + ")";
                    }
                }
            }
            txtBillingAmount.Text = SelectedBillingApplication.BillingAmount.ToString("#0.00");
            cmbStatusApp.SetSelectedValue(SelectedBillingApplication.Status);
            cmbCurrencyApp.SetSelectedValue(SelectedBillingApplication.Currency);
            cmbPayTypeApp.SetSelectedValue(SelectedBillingApplication.PayType);
            cmbOrderTypeApp.SetSelectedValue(SelectedBillingApplication.OrderType);

            txtInvoiceNo.Text = SelectedBillingApplication.InvoiceNo;
            txtCompanyTitle.Text = SelectedBillingApplication.CompanyTitle;
            txtRemark.Text = SelectedBillingApplication.Remark;

            txtApprovedRemark.Text = SelectedBillingApplication.ApprovedRemark;
            txtApprovedAmount.Text = SelectedBillingApplication.PaidAmount.ToString("#0.00");

            UICommonFunctions.SetTextBoxDate(txtCreatedDate, SelectedBillingApplication.CreatedDate, "yyyy-MM-dd hh:mm:ss");
            UICommonFunctions.SetTextBoxPersonName(txtCreatedBy, SelectedBillingApplication.CreatedBy);
            UICommonFunctions.SetTextBoxDate(txtLastModifiedDate, SelectedBillingApplication.LastModifiedDate, "yyyy-MM-dd hh:mm:ss");
            UICommonFunctions.SetTextBoxPersonName(txtLastModifiedBy, SelectedBillingApplication.LastModifiedBy);
            UICommonFunctions.SetTextBoxDate(txtApprovedDate, SelectedBillingApplication.ApprovedDate, "yyyy-MM-dd hh:mm:ss");
            UICommonFunctions.SetTextBoxPersonName(txtApprovedBy, SelectedBillingApplication.ApprovedBy);

            SetFieldsForPaidInfo();
        }
        private void SetFieldsForPaidInfo()
        {
            if (PaidDetail == null)
            {
                if (cmbCurrencyApp.SelectedValue != null)
                {
                    cmbCurrency.SelectedValue = cmbCurrencyApp.SelectedValue;
                }
                if (cmbPayTypeApp.SelectedValue != null)
                {
                    cmbPayType.SelectedValue = cmbPayTypeApp.SelectedValue;
                }
                if (cmbOrderTypeApp.SelectedValue != null)
                {
                    cmbOrderType.SelectedValue = cmbOrderTypeApp.SelectedValue;
                }
                if (mIsPaid && ThisActionType == ActionType.Modify)
                {
                    if (CommonParameter.CurrentUser.PersonEntity != null)
                    {
                        txtPaidPerson.Tag = CommonParameter.CurrentUser.PersonEntity;
                        txtPaidPerson.Text = CommonParameter.CurrentUser.PersonEntity.NameCn + " (" + CommonParameter.CurrentUser.PersonEntity.PersonCode + ")";
                    }
                }
                return;
            }
            if (PaidDetail.PaidDate.ToString("yyyy-MM-dd") == "1900-01-01" ||
                PaidDetail.PaidDate.ToString("yyyy-MM-dd") == "0001-01-01")
            {
                dtpPaidDate.EditValue = null;
            }
            else
            {
                dtpPaidDate.EditValue = PaidDetail.PaidDate;
            }
            if (mIsPaid && ThisActionType == ActionType.Modify)
            {
                if (CommonParameter.CurrentUser.PersonEntity != null)
                {
                    txtPaidPerson.Tag = CommonParameter.CurrentUser.PersonEntity;
                    txtPaidPerson.Text = CommonParameter.CurrentUser.PersonEntity.NameCn + " (" + CommonParameter.CurrentUser.PersonEntity.PersonCode + ")";
                }
            }
            else
            {
                if (string.IsNullOrEmpty(PaidDetail.PaidPerson) == false)
                {
                    MstPersonEntity person = MstPersonBs.GetMstPersonByID(PaidDetail.PaidPerson);
                    if (person != null)
                    {
                        txtPaidPerson.Tag = person;
                        txtPaidPerson.Text = person.NameCn + " (" + person.PersonCode + ")";
                    }
                }
            }
            txtPaidAmount.Text = PaidDetail.PaidAmount.ToString("#0.00");
            cmbCurrency.SetSelectedValue(PaidDetail.Currency);
            cmbPayType.SetSelectedValue(PaidDetail.PayType);
            txtInvoiceNo.Text = PaidDetail.InvoiceNo;
            txtAccountsCode.Tag = null;
            txtAccountsCode.Text = "";
            if (string.IsNullOrEmpty(PaidDetail.AccountsCode) == false)
            {
                FinAccountsEntity account = FinAccountsBs.GetByCode(PaidDetail.AccountsCode);
                if (account != null)
                {
                    txtAccountsCode.Tag = account;
                    txtAccountsCode.Text = account.AccountsCode + " " + account.AccountsName;
                }
            }
            cmbOrderType.SetSelectedValue(PaidDetail.OrderType);
            cmbPaidType.SetSelectedValue(PaidDetail.PaidType);
            txtRelatedOrderId.Text = PaidDetail.RelatedOrderId;
            cmbBankAccount.SetSelectedValue(PaidDetail.BankAccount);
            txtPaidRemark.Text = PaidDetail.Remark;
        }
        private void SetEntity()
        {
            if (txtSupplier.Tag != null && txtSupplier.Tag.GetType() == typeof(MstSupplierEntity))
            {
                SelectedBillingApplication.SupplierId = ((MstSupplierEntity)txtSupplier.Tag).SupplierId;
            }
            if (dtpApplicationDate.EditValue!=null)
            {
                SelectedBillingApplication.ApplicationDate = Convert.ToDateTime(dtpApplicationDate.EditValue.ToString());
            }
            if (txtApplicationPerson.Tag != null && txtApplicationPerson.Tag.GetType() == typeof(MstPersonEntity))
            {
                SelectedBillingApplication.ApplicationPerson = ((MstPersonEntity)txtApplicationPerson.Tag).PersonId;
            }
            SelectedBillingApplication.BillingAmount = CommonFunctions.ToDecimal(txtBillingAmount.Text);
            SelectedBillingApplication.PaidAmount = CommonFunctions.ToDecimal(txtApprovedAmount.Text);
            if (cmbCurrencyApp.SelectedValue != null && cmbCurrencyApp.SelectedValue.ToString().Trim() != "")
            {
                SelectedBillingApplication.Currency = cmbCurrencyApp.SelectedValue.ToString();
            }
            if (cmbPayTypeApp.SelectedValue != null && cmbPayTypeApp.SelectedValue.ToString().Trim() != "")
            {
                SelectedBillingApplication.PayType = cmbPayTypeApp.SelectedValue.ToString();
            }
            SelectedBillingApplication.ApprovedRemark = txtApprovedRemark.Text;
            if (ThisActionType == ActionType.New || ThisActionType == ActionType.Modify)
            {
                SelectedBillingApplication.Status = "PAYING01";
            }
            SelectedBillingApplication.CompanyTitle = txtCompanyTitle.Text;
            SelectedBillingApplication.Remark = txtRemark.Text;
            if (cmbOrderTypeApp.SelectedValue != null)
            {
                SelectedBillingApplication.OrderType = cmbOrderTypeApp.SelectedValue.ToString();
            }

            if (ThisActionType == ActionType.New)
            {
                SelectedBillingApplication.CreatedDate = DateTime.Now;
                SelectedBillingApplication.CreatedBy = CommonParameter.CurrentUser.PersonId;
            }
            if (ThisActionType == ActionType.New || ThisActionType == ActionType.Modify)
            {
                SelectedBillingApplication.LastModifiedDate = DateTime.Now;
                SelectedBillingApplication.LastModifiedBy = CommonParameter.CurrentUser.PersonId;
            }
        }
        private void SetEntityForPaidInfo()
        {
            if (PaidDetail == null)
            {
                PaidDetail = new FinPaidDetailEntity();
                PaidDetail.PaidDetailId = Guid.NewGuid().ToString();
                PaidDetail.ApplicationId = SelectedBillingApplication.ApplicationId;
            }
            if (txtSupplier.Tag != null && txtSupplier.Tag.GetType() == typeof(MstSupplierEntity))
            {
                PaidDetail.SupplierId = ((MstSupplierEntity)txtSupplier.Tag).SupplierId;
            }
            if (dtpPaidDate.EditValue!=null)
            {
                PaidDetail.PaidDate = Convert.ToDateTime(dtpPaidDate.EditValue.ToString());
            }
            if (txtPaidPerson.Tag != null && txtPaidPerson.Tag.GetType() == typeof(MstPersonEntity))
            {
                PaidDetail.PaidPerson = ((MstPersonEntity)txtPaidPerson.Tag).PersonId;
            }
            PaidDetail.PaidAmount = CommonFunctions.ToDecimal(txtPaidAmount.Text);
            if (cmbCurrency.SelectedValue != null && cmbCurrency.SelectedValue.ToString().Trim() != "")
            {
                PaidDetail.Currency = cmbCurrency.SelectedValue.ToString();
            }
            if (cmbPayType.SelectedValue != null && cmbPayType.SelectedValue.ToString().Trim() != "")
            {
                PaidDetail.PayType = cmbPayType.SelectedValue.ToString();
            }
            PaidDetail.InvoiceNo = txtInvoiceNo.Text;

            if (mIsPaid && ThisActionType == ActionType.Modify)
            {
                PaidDetail.Status = NormalStatus.STATUS03.ToString();
            }
            if (txtAccountsCode.Tag != null)
            {
                PaidDetail.AccountsCode = ((FinAccountsEntity)txtAccountsCode.Tag).AccountsCode;
            }
            if (cmbOrderType.SelectedValue != null && cmbOrderType.SelectedValue.ToString() != "")
            {
                PaidDetail.OrderType = cmbOrderType.SelectedValue.ToString();
            }
            if (cmbPaidType.SelectedValue != null && cmbPaidType.SelectedValue.ToString() != "")
            {
                PaidDetail.PaidType = cmbPaidType.SelectedValue.ToString();
            }
            PaidDetail.RelatedOrderId = txtRelatedOrderId.Text;
            PaidDetail.BankAccount = cmbBankAccount.Text;

            PaidDetail.Remark = txtPaidRemark.Text;

            PaidDetail.CreatedDate = DateTime.Now;
            PaidDetail.CreatedBy = CommonParameter.CurrentUser.PersonId;
            PaidDetail.LastModifiedDate = DateTime.Now;
            PaidDetail.LastModifiedBy = CommonParameter.CurrentUser.PersonId;
            PaidDetail.ApprovedDate = DateTime.Now;
            PaidDetail.ApprovedBy = CommonParameter.CurrentUser.PersonId;
        }
        private void BindDataGrid()
        {
            DataTable dt = FinMapIoBpaBs.GetImportByAppId(SelectedBillingApplication.ApplicationId);
            gvMain.DataSource = dt;
            mListImportItem = new List<InvImportItemEntity>();
            foreach (DataRow dr in dt.Rows)
            {
                InvImportItemEntity entity = new InvImportItemEntity(dr);
                mListImportItem.Add(entity);
            }
        }
        private bool CheckData()
        {
            if ((ThisActionType == ActionType.New || ThisActionType == ActionType.Modify) && !mIsPaid)
            {
                if (string.IsNullOrEmpty(txtSupplier.Text))
                {
                    MessageBox.Show("收款方不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return false;
                }
                if (string.IsNullOrEmpty(txtApplicationPerson.Text))
                {
                    MessageBox.Show("申请人不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return false;
                }
                if (string.IsNullOrEmpty(cmbCurrencyApp.Text))
                {
                    MessageBox.Show("货币类型不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return false;
                }
                if (string.IsNullOrEmpty(cmbPayTypeApp.Text))
                {
                    MessageBox.Show("付款类型不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return false;
                }
                if (dtpApplicationDate.EditValue == null)
                {
                    MessageBox.Show("申请日期不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return false;
                }
                if (string.IsNullOrEmpty(txtBillingAmount.Text))
                {
                    MessageBox.Show("付款金额不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return false;
                }
            }
            else if (ThisActionType == ActionType.Approve)
            {
                if (string.IsNullOrEmpty(txtApprovedAmount.Text))
                {
                    MessageBox.Show("批准金额不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return false;
                }
            }
            else if (mIsPaid && ThisActionType == ActionType.Modify)
            {
                if (string.IsNullOrEmpty(txtSupplier.Text))
                {
                    MessageBox.Show("收款人不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return false;
                }
                if (string.IsNullOrEmpty(txtPaidPerson.Text))
                {
                    MessageBox.Show("付款人不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return false;
                }
                if (string.IsNullOrEmpty(cmbCurrency.Text))
                {
                    MessageBox.Show("货币类别不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return false;
                }
                if (string.IsNullOrEmpty(cmbPayType.Text))
                {
                    MessageBox.Show("付款类型不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return false;
                }
                if (dtpPaidDate.EditValue == null)
                {
                    MessageBox.Show("付款日期不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return false;
                }
                if (string.IsNullOrEmpty(txtPaidAmount.Text))
                {
                    MessageBox.Show("付款金额不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return false;
                }
                if (string.IsNullOrEmpty(cmbOrderType.Text))
                {
                    MessageBox.Show("核算项目不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return false;
                }
                if (string.IsNullOrEmpty(cmbPaidType.Text))
                {
                    MessageBox.Show("付款方式不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return false;
                } 
            }
            return true;
        }
        private bool Save()
        {
            try
            {
                if (CheckData() == false)
                {
                    return false;
                }
                if (ThisActionType == ActionType.New)
                {
                    SetEntity();
                    bool isSuccessful = FinBillingPaidApplicationBs.SaveBillingApplication(true, SelectedBillingApplication, mListImportItem);
                    if (!isSuccessful)
                    {
                        MessageBox.Show("新增失败！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return false;
                    }
                }
                else if (ThisActionType == ActionType.Modify)
                {
                    if (mIsPaid)
                    {
                        if (Convert.ToDecimal(txtApprovedAmount.Text) != Convert.ToDecimal(txtPaidAmount.Text))
                        {
                            DialogResult dr=MessageBox.Show("财务付款金额不等于核准金额，确认继续吗?","Information",MessageBoxButtons.YesNo,MessageBoxIcon.Question);
                            if (dr == DialogResult.No)
                            {
                                return false;
                            }
                        }
                        SetEntityForPaidInfo();
                        bool isSuccessful = FinPaidDetailBs.Pay(PaidDetail, SelectedBillingApplication);
                        if (!isSuccessful)
                        {
                            MessageBox.Show("付款失败！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            return false;
                        }
                    }
                    else
                    {
                        SetEntity();
                        bool isSuccessful = FinBillingPaidApplicationBs.SaveBillingApplication(false, SelectedBillingApplication, mListImportItem);
                        if (!isSuccessful)
                        {
                            MessageBox.Show("修改失败！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            return false;
                        }
                    }
                }
                else if (ThisActionType == ActionType.Approve)
                {
                    SetEntity();
                    SelectedBillingApplication.ApprovedDate = DateTime.Now;
                    SelectedBillingApplication.ApprovedBy = CommonParameter.CurrentUser.PersonId;
                    bool isSuccessful = FinBillingPaidApplicationBs.ApproveBillingApplication(SelectedBillingApplication);
                    if (!isSuccessful)
                    {
                        MessageBox.Show("审核失败！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return false;
                    }
                }
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
        }
        private void SetPanelEnable(DevGroupControl gobx, bool enable)
        {
            if (gobx != null && gobx.Controls != null)
            {
                foreach (Control con in gobx.Controls)
                {
                    if (con.GetType() == typeof(DevTextEdit))
                    {
                        ((DevTextEdit)con).Properties.ReadOnly = !enable;
                    }
                    else if (con.GetType() == typeof(EsolComboBox))
                    {
                        con.Enabled = enable;
                    }
                    else if (con.GetType() == typeof(DevDataTimePicker))
                    {
                        con.Enabled = enable;
                    }
                }
            }
        }
        private void SetFieldsEnable()
        {
            if (mIsPaid)
            {
                SetPanelEnable(gboxApplication, false);
                SetPanelEnable(gboxApproved, false);
                gvMain.Enabled = false;
            }
            else
            {
                if (ThisActionType == ActionType.New || ThisActionType == ActionType.Modify)
                {
                    //SetPanelEnable(gboxApplication, true);
                    SetPanelEnable(gboxApproved, false);
                    SetPanelEnable(gboxPaid, false);
                }
                else if (ThisActionType == ActionType.Approve)
                {
                    SetPanelEnable(gboxApplication, false);                    
                    SetPanelEnable(gboxPaid, false);
                    gvMain.Enabled = false;
                }
            }
        }
        private DataTable GetNewDataTable(DataTable table)
        {
            DataTable dt = FinMapIoBpaBs.GetImportByBillingId("");
            if (table != null && table.Rows.Count > 0)
            {
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    DataRow row = dt.NewRow();
                    foreach (DataColumn column in table.Columns)
                    {
                        if (dt.Columns.Contains(column.ColumnName))
                        {
                            row[column.ColumnName] = table.Rows[i][column.ColumnName];
                        }
                    }
                    dt.Rows.Add(row);
                }
            }
            return dt;
        }
        #endregion

        #region Event
        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            BindComboBox();
            SetFields();
            if (txtSupplier.Tag != null && txtSupplier.Tag.GetType() == typeof(MstSupplierEntity))
            {
                SetFieldsBySupplier((MstSupplierEntity)txtSupplier.Tag);
            }
            if (mIsPaid)
            {
                btnSave.Text = "付款";
            }
            BindDataGrid();
            SetFieldsEnable();
        }
        protected override void Click_Save()
        {
            if (Save())
            {
                this.DialogResult = DialogResult.OK;
            }
        }
        protected override void Click_SaveNew()
        {
            if (Save())
            {
                ThisActionType = ActionType.New;
                SelectedBillingApplication = new FinBillingPaidApplicationEntity();
                SelectedBillingApplication.ApplicationId = Guid.NewGuid().ToString();
                SetFields();
            }
        }
        protected override void Click_Approve()
        {
            if (SelectedBillingApplication == null)
            {
                return;
            }
            SelectedBillingApplication.Status = "PAYING02";
            if (Save())
            {
                this.DialogResult = DialogResult.OK;
            }
        }
        protected override void Click_Reject()
        {
            if (SelectedBillingApplication == null)
            {
                return;
            }
            SelectedBillingApplication.Status = "PAYING03";
            if (Save())
            {
                this.DialogResult = DialogResult.OK;
            }
        }
        private void txtSupplier_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainSupplierQuery frm = new frmMaintainSupplierQuery();
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedSupplier != null)
            {
                SetFieldsBySupplier(frm.SelectedSupplier);
            }
            else
            {
                SetFieldsBySupplier(null);
            }
        }

        private void txtApplicationPerson_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainEmployeeQuery frm = new frmMaintainEmployeeQuery();
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedPerson != null)
            {
                txtApplicationPerson.Text = frm.SelectedPerson.NameCn;
                txtApplicationPerson.Tag = frm.SelectedPerson;
            }
            else
            {
                txtApplicationPerson.Text = "";
                txtApplicationPerson.Tag = null;
            }
        }

        private void txtPaidPerson_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainEmployeeQuery frm = new frmMaintainEmployeeQuery(true);
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedPerson != null)
            {
                txtPaidPerson.Text = frm.SelectedPerson.NameCn;
                txtPaidPerson.Tag = frm.SelectedPerson;
            }
            else
            {
                txtPaidPerson.Text = "";
                txtPaidPerson.Tag = null;
            }
        }

        private void txtRelatedOrderId_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainPurchaseOrderQuery frm = new frmMaintainPurchaseOrderQuery(false);
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedPurchaseOrder != null)
            {
                txtRelatedOrderId.Text = frm.SelectedPurchaseOrder.PurchaseOrderCode;
            }
            else
            {
                txtRelatedOrderId.Text = "";
            }
        }

        private void txtAccountsCode_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainFinanceAccountsCodeQuery frm = new frmMaintainFinanceAccountsCodeQuery();
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedAccount != null)
            {
                txtAccountsCode.Text = frm.SelectedAccount.AccountsName;
                txtAccountsCode.Tag = frm.SelectedAccount;
            }
            else
            {
                txtAccountsCode.Text = "";
                txtAccountsCode.Tag = null;
            }
        }

        //批准金额不能大于申请的付款金额
        private void txtApprovedAmount_EditValueChanged(object sender, EventArgs e)
        {
            if (txtApprovedAmount.EditValue!=null
                && txtBillingAmount.EditValue!=null)
            {
                if (Convert.ToDecimal(txtApprovedAmount.EditValue) > Convert.ToDecimal(txtBillingAmount.EditValue))
                {
                    MessageBox.Show("批准金额不能大于申请的付款金额！","Information",MessageBoxButtons.OK,MessageBoxIcon.Information);
                    txtApprovedAmount.EditValue = null;
                    txtApprovedAmount.Refresh();
                    return;
                }
            }
        }

        //财务付款金额不能大于核准金额
        private void txtPaidAmount_EditValueChanged(object sender, EventArgs e)
        {
            if (txtApprovedAmount.EditValue!=null
               && txtBillingAmount.EditValue!=null
                && txtPaidAmount.EditValue!=null)
            {
                if (Convert.ToDecimal(txtPaidAmount.EditValue) > Convert.ToDecimal(txtApprovedAmount.EditValue))
                {
                    MessageBox.Show("财务付款金额不能大于核准金额！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    txtPaidAmount.EditValue = null;
                    txtPaidAmount.Refresh();
                    return;
                }
            }
        }

        private void tsmNewLine_Click(object sender, EventArgs e)
        {
            DataTable dt = gvMain.DataSource as DataTable;
            DataRow dr = dt.NewRow();
            dt.Rows.Add(dr);
        }

        private void tsmDelLine_Click(object sender, EventArgs e)
        {
            if (drSelected == null)
            {
                return;
            }
            DataTable dt = gvMain.DataSource as DataTable;
            dt.Rows.Remove(drSelected);
        }

        DataRow drSelected;
        private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            drSelected = gridView1.GetDataRow(e.FocusedRowHandle);
        }

        private void repositoryItemButtonEdit1_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainImportItemsQuery frm = new frmMaintainImportItemsQuery(true);
            frm.PayStatus = "未申请";
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedDataTable != null)
            {
                mListImportItem = new List<InvImportItemEntity>();
                DataTable dt = GetNewDataTable(frm.SelectedDataTable);
                gvMain.DataSource = dt;
                foreach (DataRow dr in dt.Rows)
                {
                    InvImportItemEntity entity = new InvImportItemEntity(dr);
                    mListImportItem.Add(entity);
                }
            }
            else
            {
                mListImportItem = null;
                gvMain.DataSource = null;
            }
        }

        #endregion
    }
}
